package pile.customer.controller;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pile.customer.entity.ResponseResult;
import pile.customer.params.ReceivingInformation;
import pile.customer.service.IWeChatService;

import java.io.IOException;

@RestController
@RequestMapping("/weChat")
@Log4j2
public class WeChatController {

    @Autowired
    private IWeChatService weChatService;


    /**
     * 请求接口验证
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @GetMapping("/receive")
    public String receiveGetRequest(String signature, String timestamp, String nonce, String echostr){


        return weChatService.receiveGetRequest(signature, timestamp, nonce, echostr);
    }

    /**
     * 接受消息
     * @param requestBody
     * @return
     */
    @PostMapping("/receive")
    public String acceptMessage(
                                  String timestamp,
                                  String nonce,
                                  String msg_signature,
                                @RequestBody String requestBody) throws Exception {

        JSONObject data = JSONUtil.parseObj(requestBody);
        data.put("timestamp",timestamp);
        data.put("nonce",nonce);
        data.put("msg_signature",msg_signature);
        return weChatService.acceptMessage(data);
    }

    /**
     *新增图片素材
     * @return
     */
    @PostMapping("/uploadTempMedia")
    public ResponseResult uploadTempMedia(@RequestParam("uploadTempMedia") String uploadTempMedia,
                                          @RequestPart("file") MultipartFile file) throws IOException {


        return weChatService.uploadTempMedia(uploadTempMedia,file);
    }

    /**
     * 微信获取客服消息内的临时素材
     */
    @GetMapping("/getTempMedia")
    public ResponseResult getTempMedia(String mediaId,Integer channel) throws IOException {


        return weChatService.getTempMedia(mediaId, channel);
    }

    @PostMapping("/sendMessage")
    public ResponseResult sendMessage(@RequestBody ReceivingInformation receivingInformation)  {


        //发送给微信服务器
        return weChatService.sendMessage(receivingInformation);

    }



}
